//
//  YHMyMerchantAddViewController.m
//  YHApp
//
//  Created by -- on 2021/12/19.
//

#import "HAMerchantAddVC.h"
#import "HAMerchantAddCanalMo.h"
#import "HAWebVC.h"

@interface HAMerchantAddVC ()<UITableViewDelegate,UITableViewDataSource,DZNEmptyDataSetSource,DZNEmptyDataSetDelegate>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSMutableArray *dataArray;

@end

@implementation HAMerchantAddVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.baseTLabel.text = @"新增商户";

    [self setupUI];
    
    MJRefreshGifHeader *mj_header = [MJRefreshGifHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
    self.tableView.mj_header = mj_header;
        
    [self.tableView.mj_header beginRefreshing];

    
    // Do any additional setup after loading the view.
}

-(void)setupUI {
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.top.mas_equalTo(app_topH+44);
        make.bottom.mas_equalTo(0);
    }];
}



#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 66;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YHMyMerchantAddcellID"];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"YHMyMerchantAddcellID"];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [self setCellDict:cell.contentView model:self.dataArray[indexPath.row]];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    HAMerchantAddCanalMo *model = self.dataArray[indexPath.row];
    HAWebVC *vc = [[HAWebVC alloc]init];
    vc.tit = @"新增商户";
    vc.urlStr = model.link;
    [self.navigationController pushViewController:vc animated:YES];
}

-(void)setCellDict:(UIView *)contentView model:(HAMerchantAddCanalMo *)model {
    for (UIView *view in contentView.subviews) {
        [view removeFromSuperview];
    }
    contentView.backgroundColor = [UIColor khaBGColor];

    UIView *mainV = [UIView ha_createView];
    mainV.backgroundColor = [UIColor whiteColor];
    [contentView addSubview:mainV];
    [mainV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.top.mas_equalTo(2);
        make.bottom.mas_equalTo(0);
    }];
    
    UIImageView *RImgV = [UIImageView ha_createImageView];
    RImgV.image = [UIImage imageNamed:@"ic_cell_more"];
    [contentView addSubview:RImgV];
    [RImgV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_offset(5);
        make.height.mas_offset(10);
        make.centerY.mas_equalTo(0);
        make.right.mas_equalTo(-20);
    }];

    UILabel *titlabel = [UILabel ha_NLabelWithFont:17 textColor:@"111a34" text:model.name alignment:0];
    [contentView addSubview:titlabel];
    [titlabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(0);
        make.left.mas_equalTo(20);
        make.right.mas_equalTo(RImgV.mas_left).offset(-10);
    }];
    
}

-(void)loadNewData {

    SYRequest *request1 = [[SYRequest alloc]init];
    request1.requestUrl = [NSString stringWithFormat:@"canal/lists"];
    request1.requestMethod = SYRequestMethodPOST;

    [[SYNetworking shareSingle] hgj_asynRequestWithArray:@[request1] responseBlock:^(SYDataModel *dataModel) {
        if (dataModel.request == request1) {
            
            [self.tableView.mj_header endRefreshing];
            
            if (dataModel.err) {
                [MBProgressHUD showError:@"网络错误"];
                return;
            }
            if (dataModel.code == 1) {
                [self.dataArray removeAllObjects];
                NSArray *arr = [HAMerchantAddCanalMo mj_objectArrayWithKeyValuesArray:dataModel.data];
                [self.dataArray addObjectsFromArray:arr];
            }else{
                [MBProgressHUD showError:dataModel.msg];
            }
            
            [self.tableView reloadData];
            
        }
    }];
    
}


- (UITableView *)tableView {
    if (_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero];
        // 去掉滚动条
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.showsHorizontalScrollIndicator = NO;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.emptyDataSetSource = self;
        _tableView.emptyDataSetDelegate = self;
        _tableView.backgroundColor = [UIColor khaBGColor];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self.view addSubview:_tableView];
    }
    return _tableView;
}


- (NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}



#pragma mark - 无数据处理 DZNEmptyDataSetSource DZNEmptyDataSetDelegate
//设置title
- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView {
    NSString *title = @"暂无数据~";
    NSDictionary *attributes = @{
        NSFontAttributeName:[UIFont systemFontOfSize:14],
        NSForegroundColorAttributeName:[UIColor colorWithHexString:@"#999999"]
    };
    return [[NSAttributedString alloc] initWithString:title attributes:attributes];
}


//设置image
- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView {
    return [UIImage imageNamed:@"hl_nodata"];
}





@end
